//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.dstz.agilebpm.sys.autoconfiguration;

import cn.hutool.extra.mail.MailAccount;
import com.dstz.sys.api.jms.producer.JmsProducer;
import com.dstz.sys.simplemq.producer.SynchronousQueueProducer;
import com.dstz.sys.util.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Configuration
@EnableConfigurationProperties({MQMailConfigProperties.class, AbSimpleMessageQueueProperties.class})
public class SimpleMqAutoConfiguration {
    @Autowired
    private MQMailConfigProperties mQMailConfigProperties;

    public SimpleMqAutoConfiguration() {
    }

    @ConditionalOnExpression("'${ab.simple-mq.message-queue-type:synchronous}'.toLowerCase() == 'synchronous'")
    @Bean
    public JmsProducer synchronousQueueProducer() {
        return new SynchronousQueueProducer();
    }

    private void setEmailConfiguration() {
        MailAccount account = new MailAccount();
        account.setHost(this.mQMailConfigProperties.getSendHost());
        account.setPort(this.mQMailConfigProperties.getSendPort());
        account.setFrom(this.mQMailConfigProperties.getMailAddress());
        account.setUser(this.mQMailConfigProperties.getNickName());
        account.setPass(this.mQMailConfigProperties.getPassword());
        account.setSslEnable(this.mQMailConfigProperties.isSSL());
        EmailUtil.setAccount(account);
    }

    @Component
    class MailAccountAutoConfiguration implements ApplicationListener<ContextRefreshedEvent> {
        MailAccountAutoConfiguration() {
        }

        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            if (event.getApplicationContext().getParent() == null) {
                SimpleMqAutoConfiguration.this.setEmailConfiguration();
            }

        }
    }
}
